  /**
   * 时间格式化
   * yyyy-MM-dd HH:mm:ss  年-月-日 时:分:秒
   */
  function formatTime(date, format) {
    date = dateType(date);
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()

    month = month < 10 ? ('0' + month) : month;
    day = day < 10 ? ('0' + day) : day;
    hour = hour < 10 ? ('0' + hour) : hour;
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;

    format = format.replace('yyyy', year).replace('MM', month).replace('dd', day).replace('HH', hour).replace('mm', minute).replace('ss', second);

    return format;

    //return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':')
  }

  /**
   * 时间格式化
   */
  function formatDate(date) {
    date = dateType(date);
    let format = '';
    let time = new Date(date).getTime();
    let now = new Date().getTime();

    let value = now - time;
    if (value < 365 * 24 * 60 * 60 * 1000) {
      if (value < 30 * 24 * 60 * 60 * 1000) {
        if (value < 72 * 60 * 60 * 1000) {
          if (value < 48 * 60 * 60 * 1000) {
            if (value < 24 * 60 * 60 * 1000) {
              if (value < 60 * 60 * 1000) {
                format = value / 60 / 1000 + '';
                format = format.split(".")[0];
                if (format < 1) {
                  format = '刚刚';
                } else {
                  format = format + '分钟前';
                }
              } else {
                format = value / 60 / 60 / 1000 + '';
                format = format.split(".")[0];
                format = format + '小时前';
              }
            } else {
              format = '昨天';
            }
          } else {
            format = '前天';
          }
        } else {
          format = '1月内';
        }
      } else {
        format = formatTime(date, "MM-dd");
      }
    } else {
      format = formatTime(date, "yyyy-MM-dd");
    }
    // if (value < 60 * 60 * 1000) {
    //   format = value / 60 / 100;
    //   format = format + '分钟前'
    //   format = format.split(".")[0]
    // } else if (value < 24 * 60 * 60 * 1000) {
    //   format = value / 24 / 60 / 100;
    //   format = format + '小时前'
    //   format = format.split(".")[0]
    // } else if (value < 365 * 24 * 60 * 60 * 1000) {
    //   format = this.formatTime(date, "HH:mm");
    // } else {
    //   format = this.formatTime(date, "yyyy-MM-dd");
    // }
    return format;
  }

  
  /**
   * 时间格式化
   */
  function formatWeek(startTime, endTime) {
    var weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    var startDate = dateType(startTime);
    var endDate = dateType(endTime);
    console.log(startDate)
    var startWeek = weekDays[startDate.getDay()];
    var endWeek = weekDays[endDate.getDay()];

    var startFormatDate = formatTime(startDate, "yyyy.MM.dd");
    var startFormatTime = formatTime(startDate, "HH:mm");
    var endFormatDate = formatTime(endDate, "yyyy.MM.dd");
    var endFormatTime = formatTime(endDate, "HH:mm");

    if(startFormatDate == endFormatDate) {
      return startFormatDate + " " + startWeek + " " + startFormatTime + "~" +  endFormatTime;
    } else {
      return startFormatDate + " " + startWeek + "~" + endFormatDate + " " +  endWeek;
    }
  }

  /**
   * 时间格式化
   */
  function formatWeek2(time) {
    var weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    var date = dateType(time);
    var week = weekDays[date.getDay()];

    var ftDate = formatTime(date, "yyyy.MM.dd");
    var ftTime = formatTime(date, "HH:mm");

    return ftDate + " " + week + " " + ftTime
  }

  /**
   * 时间格式化
   */
  function formatWeek3(time) {
    var weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    var date = dateType(time);
    var week = weekDays[date.getDay()];

    var ftDate = formatTime(date, "yyyy年MM月dd日");

    return ftDate + " " + week
  }

  /**
   * 时间格式化
   */
  function formatWeek4(startTime, endTime) {
    var weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    var startDate = dateType(startTime);
    var endDate = dateType(endTime);
    console.log(startDate)
    var startWeek = weekDays[startDate.getDay()];
    var endWeek = weekDays[endDate.getDay()];

    var startFormatDate = formatTime(startDate, "MM.dd");
    var startFormatTime = formatTime(startDate, "HH:mm");
    var endFormatDate = formatTime(endDate, "MM.dd");
    var endFormatTime = formatTime(endDate, "HH:mm");

    if(startFormatDate == endFormatDate) {
      return startFormatDate + " " + startWeek + " " + startFormatTime + "~" +  endFormatTime;
    } else {
      return startFormatDate + " " + startWeek;
    }
  }

  function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }

  function dateType(date) {
    if (typeof date === "number") {
      return new Date(date);
    } else if (typeof date === "string") {
      return new Date(date.replace(/-/g, '/'));
    } else if (typeof date === "object") {
      return date;
    }
  }



module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  formatWeek: formatWeek,
  formatWeek2: formatWeek2,
  formatWeek3: formatWeek3,
  formatWeek4: formatWeek4
}